#include "pch.h"
#include "D2D1DeviceWrapper.h"

void D2D1DeviceWrapper::CreateDevice(ComPtr<ID3D12Device> d3d12Device, ComPtr<ID3D12CommandQueue> commandQueue)
{
	D2D1_FACTORY_OPTIONS options;
	ZeroMemory(&options, sizeof(D2D1_FACTORY_OPTIONS));


	D2D1CreateFactory(
		D2D1_FACTORY_TYPE_SINGLE_THREADED,
		__uuidof(ID2D1Factory2),
		&options,
		&m_d2dFactory
	);
	DWriteCreateFactory(
		DWRITE_FACTORY_TYPE_SHARED,
		__uuidof(IDWriteFactory2),
		&m_dwriteFactory
	);
	CoCreateInstance(
		CLSID_WICImagingFactory2,
		nullptr,
		CLSCTX_INPROC_SERVER,
		IID_PPV_ARGS(&m_wicFactory)
	);

	UINT d3d11DeviceFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;

	ComPtr<ID3D11Device>  d3d11Device;
	D3D11On12CreateDevice(
		d3d12Device.Get(), d3d11DeviceFlags, nullptr,
		0,
		reinterpret_cast<IUnknown**>(commandQueue.GetAddressOf()), 1,
		0,
		&d3d11Device, &m_d3d11DeviceContext, nullptr
	);

	d3d11Device->QueryInterface(IID_PPV_ARGS(&m_d3d11On12Device));

	ComPtr<IDXGIDevice3> dxgiDevice;
	d3d11Device.As(&dxgiDevice);

	m_d2dFactory->CreateDevice(dxgiDevice.Get(), &m_d2dDevice);

	m_d2dDevice->CreateDeviceContext(
		D2D1_DEVICE_CONTEXT_OPTIONS_NONE,
		&m_d2dDeviceContext
	);
}
